#include <stdio.h>
#include <stdarg.h>

double sum(int lim, ...);

int main(int argc, char const *argv[])
{
    double s, t;

    s = sum(3, 1.3, 1.6, 5.8);
    t = sum(6, 3.5, 6.8, 2.5, 1.5, 2.4, 3.9);
    printf("return value for "
           "sum(3, 1.3, 1.6, 5.8): %g\n",
           s);
    printf("return value for "
        "sum(6, 3.5, 6.8, 2.5, 1.5, 2.4, 3.9): %g\n",
        t);

    return 0;
}

double sum(int lim, ...) {
    va_list ap;     //声明一个列表储存参数
    double tot = 0;

    va_start(ap, lim);      //初始化参数列表
    for (int i = 0; i < lim; i++)
    {
        tot += va_arg(ap, double);  //访问参数列表
    }
    va_end(ap);

    return tot;
}
